/* 
ALL CENTER LEVEL INTERMEDIATE OUTCOMES
*/



clear all
set more off
capture log close

 if c(username) == "alison_a" {
        global ROOT 	"C:\Users\alison_a\Dropbox\HI_shared (1)\"
		global ROOT1 	"C:\Users\alison_a\Dropbox\HI\"
    }
if c(username) == "sonya_k" {
        global ROOT 	"C:\Users\sonya_k\Dropbox\HI_shared\"
		global ROOT1 	"C:/Users/sonya_k/Dropbox/HI/"
    }


global data 				"$ROOT/data/"
global output 				"$ROOT/endline report/draft 2"
global do					"$ROOT/do-files"
global baseline				"$ROOT1/Baseline"	
global data_constructed "C:\Users\alison_a\Dropbox\HI_shared (1)\endline report\FINAL\DATA"

*run program for tables
do "$do/stata programs for drawing tables/programs for endline tables, standardisations etc"

* use data
use "$data_constructed/HI_inst_merged.dta"



* set-up
*******************
*******************
* merge in treatment status
rename ll_inst LB_id_inst
merge m:1 LB_id_inst using "$data_constructed/treatment"
rename LB_id_inst ll_inst

* create dummy variables for treatment status
gen double itt2=grupo==1
lab var itt2 "treatment group 1 - HIM"
gen double itt3=grupo==2
lab var itt3 "treatment group 2 - HIM+FE"

order ll_inst wave grupo itt2 itt3

*Visit date to the centre
generate double Visit_year = m8_aaaa_vis
replace Visit_year = 2013 if wave == 0
generate double Visit_date = mdy(m8_mm_vis, m8_dd_vis, Visit_year)
format Visit_date %td
drop Visit_year

egen dane=mean(m0_dane), by(ll_inst)
tab dane, gen(dane_)


* general variables
*******************
*******************
*Number of children in each Centre
generate double NumChildren = m8_421
* in the one case where missing use FU value 
sum NumChildren if ll_inst==102
replace NumChildren=r(mean) if ll_inst==102

*Number of working hours per week in each centre
generate double Start_time = m8_110a1*60+m8_110a2
generate double Finish_time = m8_110b1*60+m8_110b2
generate double Working_hours = (Finish_time - Start_time)/60*5
drop Start_time Finish_time
* replace with FU value where missing
sum Working_hours if ll_inst==102
replace Working_hours=r(mean) if ll_inst==102
codebook Working_hours

*Teachers per centre - adjusted for FTE
*generate double Teachers_FTE = m11_hours_teachers/Working_hours
*label variable Teachers_FTE "Number of Teachers in the Centre, adjusted for full-time equivalent (FTE)"


* ALL INSTITUTIONAL LEVEL INTERMEDIATE OUTCOMES
*************************************************
* specify control variables for centre level outcomes 
/* to intermediate outcomes at the child care center level, in which case the set
 of covariates in X will be replaced with basic child care center characteristics 
 at baseline or basic time invariant child care center characteristics (namely,
 size, child-teacher/pedagogical staff ratio, and baseline quality score (if 
 pertinent given the outcome variable)). 
 
 - number of children at BL
 - child/teacher ratio at BL
 */
 
 * generate double teacher/student ratio !!!!!!!!!!!!!!!!!!!! correct for part-time working - LATER!!!!!!!!!
 gen double teach_stu_ratio=m11_num_teach/NumChild
 
foreach x of varlist NumChildren teach_stu_ratio {
	tempvar `x'_temp
	gen double ``x'_temp'=`x' if wave==0
	egen double `x'_0 =mean(``x'_temp'), by(ll_inst)
	}
	
global controls_centre "NumChildren_0 teach_stu_ratio_0 dane_*"

tempfile keep
save `keep'

* reading routines in centre (section from module 11 (teacher level))
***********************************************************************
keep $controls_centre ll_inst wave itt*
keep if wave==1
collapse (mean) $controls_centre itt*, by(ll_inst)
tempfile centre
save `centre'


* Learning routines (productive) and personal care routines (unproductive) 
*********************************************************************************************************
/*Se crea el promedio del n�mero de veces a la semana que la maestra realiza rutinas productivas y aparte no productivas*/


/*El formulario preguntaba en esta forma

 (1) m11_504`a'1: En la �ltima semana, usted realiz� las siguientes actividades con los ni�os/as de su clase: SI (1) / NO (2)
 (2) m11_504`a'2: �Cuantos d�as? (SI RESPONDIO SI A LA PREGUNTA ANTERIOR)
 (3) m11_504`a'3: �Cuantas veces por d�a?
 
 
 Se contruye una variable que cuando a la pregunta (1) responde NO, le asigna 0 a (2) y (3). 
 Luego se contruye el n�mero de veces por semana = (2)*(3)
 
 Nos va a interesar la media de estas variables (que ser� una para productivas y una para no productivas (cuidado personal)
 
 Productivas: 
- (c) acariciarlos o consentirlos
- (d) leer cuentos
- (e) relatar historias 
- (f) conversar
- (g) cantar
- (h) bailar
- (ii) ver un pograma o video pedagogico en televisi�n 
- (j) visitar otros lugares de la comunidad
- (k) juego libre dentro de las aulas 
- (l) juego libre en �rea recreativa
- (n) Actividades fisicas como correr, saltar


No Productivas: 
- (a) rutinas de higiene y cuidado como cambiar pa�al, lavar los dientes, lavar manos
- (b) darles medicinas/remedios
- (i) ver television
- (m) dormir, descansar
 
 
 OJO: En JS no se estaba incluyendo (que s� est�n en los formularios)
 - (o) Desarrollo de actividades grupales de aprendizaje (m11_504o1)
 - (p) Desarrollo de actividades individuales de aprendizaje (m11_504p1)
 
 Voy a crear dos rowmean diferentes para el caso de rutinas productivas. Una incluye estas dos variables, otra no (igual a la hecha antes) (para elegir)
 
 */
  
* create BL variables
 use "$baseline/Bases/Instituciones/Modulo11 TODAS MAESTRAS.dta", clear

 keep ll_maest ll_inst m11_504* m11_505*

	global letras "a b c d e f g h i  j k l m n o p"

	*Recode de la pregunta (2) y (3) = 0 si (1)==NO (valor 2) 

	foreach x in $letras {
		gen double rec_m11_504`x'2 = m11_504`x'2
		replace rec_m11_504`x'2 = 0 if m11_504`x'1==2
		label var rec_m11_504`x'2 "recode m11_504`x'2 - Asigna 0 si m11_504`x'1=NO"
		gen double rec_m11_504`x'3 = m11_504`x'3
		replace rec_m11_504`x'3 = 0 if m11_504`x'1==2
		label var rec_m11_504`x'3 "recode m11_504`x'3 - Asigna 0 si m11_504`x'1=NO"
		}


	*Genera el n�mero de veces por semana (2)*(3)

	foreach x in $letras {
		gen double vecesporsemana_m11_504`x' = rec_m11_504`x'2*rec_m11_504`x'3
		recode vecesporsemana_m11_504`x' (.=0) 
		label var vecesporsemana_m11_504`x' "Number of times per week she performs activity m11_504`x'"
		}

	*La pregunta 505 funciona igual. Se hace el mismo recode.

	global letras2 "a b c d e f g h i j k l m n o p q r s"

	d m11_505*
	
	*Recode de la pregunta (2) y (3) = 0 si (1)==NO (valor 2) 
	gen double m11_505f2 =1 // missing variable
	foreach x in $letras2 {
		gen double rec_m11_505`x'2 = m11_505`x'2
		replace rec_m11_505`x'2 = 0 if m11_505`x'1==2
		label var rec_m11_505`x'2 "recode m11_505`x'2 - Asigna 0 si m11_505`x'1=NO"
		gen double rec_m11_505`x'3 = m11_505`x'3
		replace rec_m11_505`x'3 = 0 if m11_505`x'1==2
		label var rec_m11_505`x'3 "recode m11_505`x'3 - Asigna 0 si m11_505`x'1=NO"
		}


	*Genera el n�mero de veces por semana (2)*(3)

	foreach x in $letras2 {
		gen double vecesporsemana_m11_505`x' = rec_m11_505`x'2*rec_m11_505`x'3
		recode vecesporsemana_m11_505`x' (.=0) 
		label var vecesporsemana_m11_505`x' "Number of times per week she performs activity m11_505`x'"
		}
		
		
		*Genera el promedio por turina productiva / no productiva
		
		*No productiva
	egen double rutinas_NP = rowmean(vecesporsemana_m11_504a vecesporsemana_m11_504b vecesporsemana_m11_504i vecesporsemana_m11_504m)
	label var rutinas_NP "Average times per week she performs non-productive routines"

		/*Productiva (NO incluir actividades grupales (m11_504o1) actividades individuales (ni m11_504p1)
	egen double rutinas_P1 = rowmean(vecesporsemana_m11_504c vecesporsemana_m11_504d vecesporsemana_m11_504e vecesporsemana_m11_504f vecesporsemana_m11_504g vecesporsemana_m11_504h vecesporsemana_m11_504j vecesporsemana_m11_504k vecesporsemana_m11_504l vecesporsemana_m11_504n )
	label var rutinas_P1 "Average times per week it performs learning routines (no incluye actividades grupales e individuales)"*/

		*Productiva (incluye actividades grupales (m11_504o1) actividades individuales (ni m11_504p1)
	egen double rutinas_P1 = rowmean(vecesporsemana_m11_504c vecesporsemana_m11_504d vecesporsemana_m11_504e vecesporsemana_m11_504f vecesporsemana_m11_504g vecesporsemana_m11_504h vecesporsemana_m11_504j vecesporsemana_m11_504k vecesporsemana_m11_504l vecesporsemana_m11_504n vecesporsemana_m11_504o vecesporsemana_m11_504p)
	label var rutinas_P1 "Average times per week she performs productive learning routines (no incluye preguntas 505*)"

	egen double rutinas_P2 = rowmean(vecesporsemana_m11_504c vecesporsemana_m11_504d vecesporsemana_m11_504e vecesporsemana_m11_504f vecesporsemana_m11_504g vecesporsemana_m11_504h vecesporsemana_m11_504j vecesporsemana_m11_504k vecesporsemana_m11_504l vecesporsemana_m11_504n vecesporsemana_m11_504o vecesporsemana_m11_504p vecesporsemana_m11_505a vecesporsemana_m11_505b vecesporsemana_m11_505c vecesporsemana_m11_505d vecesporsemana_m11_505e vecesporsemana_m11_505f vecesporsemana_m11_505g vecesporsemana_m11_505h vecesporsemana_m11_505i vecesporsemana_m11_505j vecesporsemana_m11_505k vecesporsemana_m11_505l vecesporsemana_m11_505m vecesporsemana_m11_505n vecesporsemana_m11_505o vecesporsemana_m11_505p vecesporsemana_m11_505q vecesporsemana_m11_505r vecesporsemana_m11_505s)
	label var rutinas_P2 "Average times per week she performs productive learning routines (incluye preguntas 505*)"

	sum rutinas*, detail
	
	* collapse at inst level
	collapse (mean) rutinas_NP rutinas_P1 rutinas_P2 vecesporsemana_m11_505d, by(ll_inst)
	rename rutinas* rutinas*_0
	rename vecesporsemana_m11_505d vecesporsemana_m11_505d_0
	
	tempfile routines_bl
	save `routines_bl'
	
	
* teacher datasets (FU) - routines and trainings data
************************
use "$data/endline raw/01 INSTITUCIONES/Modulo11 - Maestras Hogar Infantil/a478_m11a_maestra.dta", clear
keep ll_maest ll_inst m11_504* m11_505* m11_204* 

global letras "a b c d e f g h i ii j k l m n o p"
*Recode de la pregunta (2) y (3) = 0 si (1)==NO (valor 2) 

foreach x in $letras {
	gen double rec_m11_504`x'2 = m11_504`x'2
	replace rec_m11_504`x'2 = 0 if m11_504`x'1==2
	label var rec_m11_504`x'2 "recode m11_504`x'2 - Asigna 0 si m11_504`x'1=NO"
	gen double rec_m11_504`x'3 = m11_504`x'3
	replace rec_m11_504`x'3 = 0 if m11_504`x'1==2
	label var rec_m11_504`x'3 "recode m11_504`x'3 - Asigna 0 si m11_504`x'1=NO"
	}


*Genera el n�mero de veces por semana (2)*(3)

foreach x in $letras {
	gen double vecesporsemana_m11_504`x' = rec_m11_504`x'2*rec_m11_504`x'3
	recode vecesporsemana_m11_504`x' (.=0) 
	label var vecesporsemana_m11_504`x' "Number of times per week she performs activity m11_504`x'"
	}

*La pregunta 505 funciona igual. Se hace el mismo recode.

global letras2 "a b c d e f g h i j k l m n o p q r s"


*Recode de la pregunta (2) y (3) = 0 si (1)==NO (valor 2) 

foreach x in $letras2 {
	gen double rec_m11_505`x'2 = m11_505`x'2
	replace rec_m11_505`x'2 = 0 if m11_505`x'1==2
	label var rec_m11_505`x'2 "recode m11_505`x'2 - Asigna 0 si m11_505`x'1=NO"
	gen double rec_m11_505`x'3 = m11_505`x'3
	replace rec_m11_505`x'3 = 0 if m11_505`x'1==2
	label var rec_m11_505`x'3 "recode m11_505`x'3 - Asigna 0 si m11_505`x'1=NO"
	}


*Genera el n�mero de veces por semana (2)*(3)

foreach x in $letras2 {
	gen double vecesporsemana_m11_505`x' = rec_m11_505`x'2*rec_m11_505`x'3
	recode vecesporsemana_m11_505`x' (.=0) 
	label var vecesporsemana_m11_505`x' "Number of times per week she performs activity m11_505`x'"
	}
	
	
	*Genera el promedio por turina productiva / no productiva
	
	*No productiva
egen double rutinas_NP = rowmean(vecesporsemana_m11_504a vecesporsemana_m11_504b vecesporsemana_m11_504i vecesporsemana_m11_504m)
label var rutinas_NP "Personal care routines (Non-productive)"

	/*Productiva (NO incluir actividades grupales (m11_504o1) actividades individuales (ni m11_504p1)
egen double rutinas_P1 = rowmean(vecesporsemana_m11_504c vecesporsemana_m11_504d vecesporsemana_m11_504e vecesporsemana_m11_504f vecesporsemana_m11_504g vecesporsemana_m11_504h vecesporsemana_m11_504j vecesporsemana_m11_504k vecesporsemana_m11_504l vecesporsemana_m11_504n )
label var rutinas_P1 "Average times per week it performs learning routines (no incluye actividades grupales e individuales)"*/

	*Productiva (incluye actividades grupales (m11_504o1) actividades individuales (ni m11_504p1)
egen double rutinas_P1 = rowmean(vecesporsemana_m11_504c vecesporsemana_m11_504d vecesporsemana_m11_504e vecesporsemana_m11_504f vecesporsemana_m11_504g vecesporsemana_m11_504h vecesporsemana_m11_504j vecesporsemana_m11_504k vecesporsemana_m11_504l vecesporsemana_m11_504n vecesporsemana_m11_504o vecesporsemana_m11_504p)
label var rutinas_P1 "Learning routines (no incluye preguntas 505*)"

egen double rutinas_P2 = rowmean(vecesporsemana_m11_504c vecesporsemana_m11_504d vecesporsemana_m11_504e vecesporsemana_m11_504f vecesporsemana_m11_504g vecesporsemana_m11_504h vecesporsemana_m11_504j vecesporsemana_m11_504k vecesporsemana_m11_504l vecesporsemana_m11_504n vecesporsemana_m11_504o vecesporsemana_m11_504p vecesporsemana_m11_505a vecesporsemana_m11_505b vecesporsemana_m11_505c vecesporsemana_m11_505d vecesporsemana_m11_505e vecesporsemana_m11_505f vecesporsemana_m11_505g vecesporsemana_m11_505h vecesporsemana_m11_505i vecesporsemana_m11_505j vecesporsemana_m11_505k vecesporsemana_m11_505l vecesporsemana_m11_505m vecesporsemana_m11_505n vecesporsemana_m11_505o vecesporsemana_m11_505p vecesporsemana_m11_505q vecesporsemana_m11_505r vecesporsemana_m11_505s)
label var rutinas_P2 "Learning routines (productive)"

label var vecesporsemana_m11_505d "Reading stories"
sum rutinas*, detail

* merge in bl
merge m:1 ll_inst using `routines_bl'

* merge in centre level controls
drop _merge 
merge m:1 ll_inst using `centre'

* tables
myttests  rutinas_NP rutinas_P2 vecesporsemana_m11_505d, bl(y) itt2(itt2) itt3(itt3) controls($controls_centre)
table_2s, filename(teacher_routines)

* teacher's training
gen double training_studied=(m11_204a==1) if m11_204a~=.
gen double training_completed=(m11_204b==1) if m11_204a~=.

label var training_studied "Studied for Diploma"
label var training_completed "Completed Diploma"

tab training_studied if itt3==1
tab training_completed if itt3==1

* tables
myttests_process  training_studied training_completed ,  itt2(itt2) itt3(itt3) 
table_process2, filename(training)

****************************************************************************************************************************************************************************teach_exp_0 total_burnout_0 jobsatisfaction_0 depresion_0
***Teacher satisfaction, depression, burn-out: BASELINE
 use "$baseline/Bases/Instituciones/Modulo11 TODAS MAESTRAS.dta", clear
 keep ll_maest ll_inst m11_308* m11_803* m11_801* m11_116a
 
 **FOLLOWING ALISON'S CODE FOR CONSTRUCTING WELL-BEING, JOB SATISFACTION AND BURNOUT SCORES FROM CHILD LEVEL FILE
	*BL teacher experience
	gen expmaestra_0=m11_116a
	gen teach_exp_0=(expmaestra_0>=r(p50)) if expmaestra_0~=.
 
 	* BL wellbeing / depression
	gl letras1 "a b d e f g i j"
	gl letras2 "c h"

	foreach x in $letras1 {
				recode m11_801`x' (1=0) (2=1)(3=2)(4=3), gen(rec_m11_801`x')
				}
				
	foreach x in $letras2 {
				recode m11_801`x' (1=3) (2=2)(3=1)(4=0), gen(rec_m11_801`x')
				}	
				
	*Puntaje depresi�n
	egen depresion_0 = rowtotal(rec_m11_801a-rec_m11_801h)
	label var depresion_0 "Depression score" 

	
	* BL job-satisfaction
	gl letras_1 "a b f g i k m p r s v x "
	gl letras_2 "c d e h j l n o q t u w"

	foreach x in $letras_1 {
				gen rec_m11_308`x'= m11_308`x'
				label var rec_m11_308`x' "m11_308`x'"
				}
				
	foreach x in $letras_2 {
				recode m11_308`x' (1=5) (2=4)(3=3)(4=2)(5=1), gen(rec_m11_308`x')
				}	

	egen jobsatisfaction_0=rowtotal(rec_m11_308*)
	label var jobsatisfaction_0 "job satisfaction (combining all categories)"
			
	* BL job burnout
	
	*Recode de los items a-l

	gl letras "a b c d e f g h i j k l"

	foreach x in $letras {
				recode m11_803`x' (1=0) (2=1)(3=2)(4=3)(5=4)(6=5)(7=6), gen(rec_m11_803`x')
				}
	egen total_burnout_0=rowtotal(rec_m11_803a rec_m11_803b rec_m11_803e rec_m11_803h rec_m11_803l rec_m11_803c rec_m11_803g rec_m11_803i rec_m11_803d rec_m11_803f rec_m11_803j rec_m11_803k)			
	label var total_burnout_0 "total burnout score (emotional burnout, depersonalisation and  personal accomp combined)"	

	keep ll_inst teach_exp_0 total_burnout_0 jobsatisfaction_0 depresion_0

	* collapse at inst level
	collapse (mean) teach_exp_0 total_burnout_0 jobsatisfaction_0 depresion_0, by(ll_inst)
	
	
	tempfile tchrwellbeing_bl
	save `tchrwellbeing_bl'

			
****************************************************************************************************************************************************************************teach_exp_0 total_burnout_0 jobsatisfaction_0 depresion_0
***Teacher satisfaction, depression, burn-out: ENDLINE

use "$data/endline raw/01 INSTITUCIONES/Modulo11 - Maestras Hogar Infantil/a478_m11a_maestra", clear	

	keep ll_maest ll_inst m11_308* m11_803* m11_801* j_dane
	ren j_dane dane
	tab dane, gen(dane_) 

 
  	* FU wellbeing / depression
	gl letras1 "a b d e f g i j"
	gl letras2 "c h"

	foreach x in $letras1 {
				recode m11_801`x' (1=0) (2=1)(3=2)(4=3), gen(rec_m11_801`x')
				}
				
	foreach x in $letras2 {
				recode m11_801`x' (1=3) (2=2)(3=1)(4=0), gen(rec_m11_801`x')
				}	
				
	* FU depresi�n
	egen depresion = rowtotal(rec_m11_801a-rec_m11_801h)
	label var depresion "Depression score" 

	
	* FU job-satisfaction
	gl letras_1 "a b f g i k m p r s v x "
	gl letras_2 "c d e h j l n o q t u w"

	foreach x in $letras_1 {
				gen rec_m11_308`x'= m11_308`x'
				label var rec_m11_308`x' "m11_308`x'"
				}
				
	foreach x in $letras_2 {
				recode m11_308`x' (1=5) (2=4)(3=3)(4=2)(5=1), gen(rec_m11_308`x')
				}	

	egen jobsatisfaction=rowtotal(rec_m11_308*)
	label var jobsatisfaction "job satisfaction (combining all categories)"
			
	* FU job burnout
	
	*Recode de los items a-l

	gl letras "a b c d e f g h i j k l"

	foreach x in $letras {
				recode m11_803`x' (1=0) (2=1)(3=2)(4=3)(5=4)(6=5)(7=6), gen(rec_m11_803`x')
				}
	egen total_burnout=rowtotal(rec_m11_803a rec_m11_803b rec_m11_803e rec_m11_803h rec_m11_803l rec_m11_803c rec_m11_803g rec_m11_803i rec_m11_803d rec_m11_803f rec_m11_803j rec_m11_803k)			
	label var total_burnout "total burnout score (emotional burnout, depersonalisation and  personal accomp combined)"	
	
	keep ll_inst ll_maest dane total_burnout jobsatisfaction depresion
	

***************************************************************************************************************************************************************************teach_exp_0 total_burnout_0 jobsatisfaction_0 depresion_0
 
 * merge in bl
 merge m:1 ll_inst using `tchrwellbeing_bl'

* merge in centre level controls
drop _merge 
merge m:1 ll_inst using `centre'



label var jobsatisfaction 	"Teacher job-satisfaction"
label var total_burnout 	"Teacher burnout"
label var depresion 		"Teacher depression"

corr jobsatisfaction_0 jobsatisfaction total_burnout_0 total_burnout depresion_0 depresion

*teacher experience variable indicates the proportion of teachers with experience above 50%
gen expstaff_prop_0 = teach_exp_0>0.5

* standardise all measures relative to control : AA 11.02.16
foreach x in jobsatisfaction total_burnout depresion {
	sum `x' if itt2==0 & itt3==1
	gen `x'_z=(`x'-r(mean))/r(sd)
	sum `x'_0 
	gen `x'_z_0=(`x'_0-r(mean))/r(sd)
	}

label var jobsatisfaction_z 	"Teacher job-satisfaction"
label var total_burnout_z 	"Teacher burnout"
label var depresion_z 		"Teacher depression"

* tables
myttests  jobsatisfaction_z total_burnout_z depresion_z, bl(y) itt2(itt2) itt3(itt3) controls($controls_centre)
table_std_2s, filename(teacher_wellbeing)



/*
*het
	ren jobsatisfaction 	jobsat
	ren jobsatisfaction_0 	jobsat_0
	global vars "jobsat total_burnout depresion"
	
	 *setup
	hetero_setup $vars, hetero(expstaff_prop_0) label0("Less experienced teachers") label1("More experienced teachers")
	set trace off	
	hetero $vars , bl(y) hvars(expstaff_prop_0) itt2(itt2) itt3(itt3) cluster(ll_inst ) controls($controls_centre) sided(2)	
	table_het_raq, filename(teacher_wellbeing_exp)
*/


****************************************************************************************************************************************************************************
****TSEEQ = Teacher Survey of Early Education Quality (sub-scales: Evaluation, Teaching, Planning, interaction, leadership)
*BASELINE
 use "$baseline/Bases/Instituciones/Modulo11 TODAS MAESTRAS.dta", clear
 keep ll_maest ll_inst m11_4-m11_58
 
*evaluation 1
factor m11_4 m11_5 m11_6, factor(1) 
predict tseeq_eval1

*evaluation 2
factor m11_1 m11_32 m11_36 m11_54 m11_67 m11_72 m11_75 m11_93 m11_43 m11_56 m11_58, factor(1)
predict tseeq_eval2

*teaching
factor m11_25 m11_26 m11_27 m11_28 m11_29 m11_31 m11_33 m11_34 m11_37 m11_39 m11_41 m11_42, factor(1)
predict tseeq_teach

*study plans
factor m11_44 m11_45 m11_46 m11_47 m11_49 m11_55 m11_59 m11_60 m11_62 m11_63 m11_71 m11_78 m11_79, factor(1)
predict tseeq_plan

*interaction
factor m11_80 m11_82 m11_83 m11_86 m11_87 m11_88 m11_89 m11_90 m11_91, factor(1)
predict tseeq_interact

*leadership
factor m11_92 m11_94 m11_102, factor(1)
predict tseeq_lead

* collapse at inst level
 collapse (mean) tseeq_*, by(ll_inst)
 foreach i in tseeq_eval1 tseeq_eval2 tseeq_teach tseeq_plan tseeq_interact tseeq_lead {
	ren `i' `i'_0
}
 	
	tempfile tseeq_bl
	save `tseeq_bl'

*FOLLOW-UP
use "$data/endline raw/01 INSTITUCIONES/Modulo11 - Maestras Hogar Infantil/a478_m11a_maestra", clear	
keep ll_maest ll_inst m11_4-m11_58 j_dane
 ren j_dane dane
 tab dane, gen(dane_) 

*evaluation 1
factor m11_4 m11_5 m11_6, factor(1) 
predict tseeq_eval1

*evaluation 2
factor m11_1 m11_32 m11_36 m11_54 m11_67 m11_72 m11_75 m11_93 m11_43 m11_56 m11_58, factor(1)
predict tseeq_eval2

*teaching
factor m11_25 m11_26 m11_27 m11_28 m11_29 m11_31 m11_33 m11_34 m11_37 m11_39 m11_41 m11_42, factor(1)
predict tseeq_teach

*study plans
factor m11_44 m11_45 m11_46 m11_47 m11_49 m11_55 m11_59 m11_60 m11_62 m11_63 m11_71 m11_78 m11_79, factor(1)
predict tseeq_plan

*interaction
factor m11_80 m11_82 m11_83 m11_86 m11_87 m11_88 m11_89 m11_90 m11_91, factor(1)
predict tseeq_interact

*leadership
factor m11_92 m11_94 m11_102, factor(1)
predict tseeq_lead

* merge in bl
 merge m:1 ll_inst using `tseeq_bl'

* merge in centre level controls
drop _merge 
merge m:1 ll_inst using `centre'

label var tseeq_eval1 		"TSEEQ: evaluation 1"
label var tseeq_eval2 		"TSEEQ: evaluation 2"
label var tseeq_teach 		"TSEEQ: teaching"
label var tseeq_plan 		"TSEEQ: planning"
label var tseeq_interact 	"TSEEQ: interacting"
label var tseeq_lead 	    "TSEEQ: leadership"

myttests  tseeq_eval1 tseeq_eval2 tseeq_teach tseeq_plan tseeq_interact tseeq_lead, bl(y) itt2(itt2) itt3(itt3) controls($controls_centre)
table_2s, filename(teacher_tseeq)


*******************************************
*******************************************

*******************************************
*******************************************

 * STAFFING PROCESS VARIABLES
*************************

use `keep', clear
*Socio-Emotional Professional
*******************************

generate double NumOfSocEmot_m8 = m8_420c2
label var NumOfSocEmot_m8 "number of SE professionals as reported in m8 (q420)"
label var m16_num_socioemot "number of SE professionals as reported in m16"
* replace with 0 if missing
replace NumOfSocEmot_m8=0 if NumOfSocEmot_m8==.
replace m16_num_socioemot=0 if m16_num_socioemot==.

tab NumOfSocEmot_m8 m16_num_socioemot
codebook NumOfSocEmot_m8 m16_num_socioemot

gen double q1002_se=(m8_1002c==1) if wave==1
tab q1002_se m16_num_socioemot if wave==1

* take max - only one place where they couldn't find the SE to interview them
gen double staffing_se_num=max(q1002_se, m16_num_socioemot)
*gen double staffing_se_num=m16_num_socioemot
codebook staffing_se_num
label var staffing_se_num "number of SE experts in centre (max of two reports - m8 and m16)"
generate double staffing_se_hired = (staffing_se_num>0)
label var staffing_se_hired "=1 if centre has at least one SE expert"

* generate double FTE
replace m16_hours_socioemot=0 if m16_hours_socioemot==.
generate double staffing_se_fte = m16_hours_socioemot/Working_hours
* if very close to full time or half time but just under then adjust
replace staffing_se_fte=1 if staffing_se_fte<1 & staffing_se_fte>=0.8
replace staffing_se_fte=2 if staffing_se_fte<2 & staffing_se_fte>=1.6
replace staffing_se_fte=3 if staffing_se_fte<3 & staffing_se_fte>=2.4
label var staffing_se_fte "Number of FTE SE experts"

* ratio
generate double staffing_se_ratio_fte = staffing_se_fte/NumChildren
label var staffing_se_ratio_fte "ratio of SE (FTE) to children"

generate double staffing_se_ratio = staffing_se_num/NumChildren
label var staffing_se_ratio "ratio of SE to children"

* ICBF ratio
gen double staffing_se_guideline_fte = (staffing_se_ratio_fte >= 0.005)
replace staffing_se_guideline_fte=1 if staffing_se_ratio_fte==0.005

label var staffing_se_guideline_fte "=1 if at least one SE (FTE) per 200 children"

gen double staffing_se_guideline = (staffing_se_ratio >= 0.005)
label var staffing_se_guideline "=1 if at least one SE  per 200 children"


* SE expert start date
generate double staffing_se_startdate = mdy(m8_1102c4b,m8_1102c4a,m8_1102c4c)
label var staffing_se_startdate "se expert start date"
generate double staffing_se_monthsonjob = mofd(Visit_date) - mofd(staffing_se_startdate)
label var staffing_se_monthsonjob "Number of months SE expert has had job"


*Teaching Assistant
*************************

 * look at extra data from m8 on teaching assistants - listing in 1001 of directors module
 preserve
use "$data/endline raw/01 INSTITUCIONES/Modulo8 - Director del Hogar Infantil/a478_m08c_director.dta", clear
keep if  m8_1001_3==2
keep   m81001_6* ll_inst
bys ll_inst: gen double n=_n
reshape wide m81001_6* , i(ll_inst) j(n)
egen double num_ta=rownonmiss( m81001_6c*)
gen wave=1
keep num_ta ll_inst wave
tempfile temp
save `temp'
 restore
 
 drop _merge
 merge 1:1 ll_inst wave using `temp'
 
 tab num_ta m15_num_tassistants
 
replace m8_420d2=0 if m8_420d2==.
replace m15_num_tassistants=0 if m15_num_tassistants==.
tab m15_num_tassistants m8_420d2
gen double staffing_ta_num=max(num_ta, m15_num_tassistants)

codebook staffing_ta_num
label var staffing_ta_num "number of TAs in centre (from directors module only - big discrepancy between this and num administed m15)"
generate double staffing_ta_hired = (staffing_ta_num>0)
label var staffing_ta_hired "=1 if centre has at least one TA"


/* generate  FTE - */
replace m15_hours_=0 if m15_hours_==.
generate  double staffing_ta_fte =m15_hours_*staffing_ta_num/m15_num_tassistants  if m15_num_tassistants~=0 & wave==1
codebook staffing_ta_fte
* three cases where there are TAs recorded by director but we don't have working hours
tempvar temp1
gen double `temp1'=m15_hours_/m15_num_tassistants if m15_num_tassistants~=0
sum `temp1'
replace staffing_ta_fte=r(mean)*staffing_ta_num if m15_num_tassistants==0
replace staffing_ta_fte=staffing_ta_fte/Working_hours
label var staffing_ta_fte "Number of FTE TAs"
replace staffing_ta_fte=1 if staffing_ta_fte<1 & staffing_ta_fte>=0.8
replace staffing_ta_fte=2 if staffing_ta_fte<2 & staffing_ta_fte>=1.6
replace staffing_ta_fte=3 if staffing_ta_fte<3 & staffing_ta_fte>=2.5
replace staffing_ta_fte=4 if staffing_ta_fte<4 & staffing_ta_fte>=3.5
replace staffing_ta_fte=5 if staffing_ta_fte<5 & staffing_ta_fte>=4.5
replace staffing_ta_fte=6 if staffing_ta_fte<6 & staffing_ta_fte>=5.5
replace staffing_ta_fte=7 if staffing_ta_fte<7 & staffing_ta_fte>=6.5
replace staffing_ta_fte=8 if staffing_ta_fte<8 & staffing_ta_fte>=7.5
replace staffing_ta_fte=9 if staffing_ta_fte<9 & staffing_ta_fte>=8.5


* ratio
generate double  staffing_ta_ratio_fte = staffing_ta_fte/NumChildren
label var staffing_ta_ratio_fte "ratio of TAs (FTE) to children"

generate double  staffing_ta_ratio = staffing_ta_num/NumChildren
label var staffing_ta_ratio "ratio of TAs to children"

* ICBF ratio
gen double  staffing_ta_guideline_fte = (staffing_ta_ratio_fte >= 0.02)
replace staffing_ta_guideline_fte=1 if staffing_ta_ratio_fte==0.02
label var staffing_ta_guideline_fte "=1 if at least one TAs (FTE) per 50 children"

gen double staffing_ta_guideline = (staffing_ta_ratio >= 0.02)
replace staffing_ta_guideline=1 if staffing_ta_ratio==0.02
label var staffing_ta_guideline "=1 if at least one TAs  per 50 children"


* most recent TA start date
* look at extra data from m8 on teaching assistants - listing in 1001 of directors module
 preserve
use "$data/endline raw/01 INSTITUCIONES/Modulo8 - Director del Hogar Infantil/a478_m08c_director.dta", clear
keep if  m8_1001_3==2
keep   m81001_6* ll_inst
gen double staffing_ta_startdate = mdy(m81001_6b,m81001_6a,m81001_6c)
format staffing_ta_startdate %td
gsort ll_inst - staffing_ta_startdate
by ll_inst: gen double n=_n
keep if n==1
label var staffing_ta_startdate "Start date of most recent TA"
gen double wave=1
keep staffing_ta ll_inst wave
tempfile temp
save `temp'
 restore
 
 drop _merge
 merge 1:1 ll_inst wave using `temp'
 drop _merge

 
 * Nutritionist
*******************************
generate double NumOfNutr_m8 = m8_420b2
label var NumOfNutr_m8 "number of nutritionists as reported in m8"
label var m17_num_nutri "number of nutritionists as reported in m17"
replace NumOfNutr_m8=0 if NumOfNutr_m8==.
replace m17_num_nutri=0 if m17_num_nutri==.
tab NumOfNutr_m8 m17_num_nutri
codebook NumOfNutr_m8 m17_num_nutri

gen double q1002_ne=(m8_1002b==1) if wave==1
tab q1002_ne m17_num_nutri if wave==1

* take max - only one place where they couldn't find the SE to interview them
gen double staffing_ne_num=max(q1002_ne, m17_num_nutri)
* use max of 2
*gen double staffing_ne_num=max(NumOfNutr_m8, m17_num_nutri)
*gen double staffing_ne_num=m17_num_nutri
codebook staffing_ne_num
label var staffing_ne_num "number of nutritionists in centre (max of two reports - m8 and m17)"
generate double staffing_ne_hired = (staffing_ne_num>0)
label var staffing_ne_hired "=1 if centre has at least one nutritionists"

* generate double FTE
replace m17_hours_=0 if m17_hours_==.
generate double staffing_ne_fte = m17_hours_/Working_hours
replace staffing_ne_fte=1 if staffing_ne_fte<1 & staffing_ne_fte>=0.8
replace staffing_ne_fte=2 if staffing_ne_fte<2 & staffing_ne_fte>=1.6
replace staffing_ne_fte=3 if staffing_ne_fte<3 & staffing_ne_fte>=2.4
label var staffing_ne_fte "Number of FTE nutritionists"

* ratio
generate double staffing_ne_ratio_fte = staffing_ne_fte/NumChildren
label var staffing_ne_ratio_fte "ratio of nutritionists (FTE) to children"

generate double staffing_ne_ratio = staffing_ne_num/NumChildren
label var staffing_ne_ratio "ratio of nutritionists to children"

* ICBF ratio
gen double staffing_ne_guideline_fte = (staffing_ne_ratio_fte >= 0.005)
label var staffing_ne_guideline_fte "=1 if at least one nutritionist (FTE) per 200 children"

gen double staffing_ne_guideline = (staffing_ne_ratio >= 0.005)
label var staffing_ne_guideline "=1 if at least one nutritionist  per 200 children"

*  start date
generate double staffing_ne_startdate = mdy(m8_1102b4b,m8_1102b4a,m8_1102b4c)
label var staffing_ne_startdate "nutrionist start date"
generate double staffing_ne_monthsonjob = mofd(Visit_date) - mofd(staffing_ne_startdate)
label var staffing_ne_monthsonjob "Number of months nutritionist has had job"

* compliance - correct ratios for all three professionals
gen double staffing_all_guideline=(staffing_ne_guideline==1 & staffing_se_guideline==1 & staffing_ta_guideline==1)
label var staffing_all_guideline "Centre meets all HIM staffing guidelines"

gen double staffing_all_guideline_fte=(staffing_ne_guideline_fte==1 & staffing_se_guideline_fte==1 & staffing_ta_guideline_fte==1)
label var staffing_all_guideline_fte "Centre meets all HIM staffing guidelines (FTE)"

* draw  table for hiring and adequate ratios
preserve
keep if wave==1
set trace off

local se "SE"
local ne "NT"
local ta "PA"
local nse "200"
local nne "200"
local nta "50"

foreach x in se ta ne {
	label var staffing_`x'_hired "At least one ``x'' present"
	label var staffing_`x'_num "Number of ``x'' present"
	label var staffing_`x'_fte "Number of ``x'' (FTE) present"
	label var staffing_`x'_guideline "At least one ``x'' present per `n`x'' children"
	label var staffing_`x'_guideline_fte "At least one ``x'' (FTE) present per `n`x'' children"
	}

	
gen double staffing_all_hired=(staffing_se_hired==1 & staffing_ne_hired==1 & staffing_ta_hired==1)
label var staffing_all_hired "Center has at least one of each professional"
	
myttests_process staffing_se_hired staffing_se_num staffing_se_fte staffing_se_guideline staffing_se_guideline_fte staffing_ne_hired staffing_ne_num staffing_ne_fte staffing_ne_guideline staffing_ne_guideline_fte staffing_ta_hired staffing_ta_num staffing_ta_fte staffing_ta_guideline staffing_ta_guideline_fte staffing_all_hired staffing_all_guideline staffing_all_guideline_fte, itt2(itt2) itt3(itt3) 
table_process, filename(staffing)
restore


* reading program
**********************
* number of books in HI, received books from FL since BL, how many books received, training from FL since BL
gen books_num=m8_701  
label var books_num "Number of children's books in centre"
myttests_process books_num, itt2(itt2) itt3(itt3) 
table_process2, filename(reading_program)
